﻿--HEADER Add [TradeValidationData]
CREATE TABLE [TradingData].[OriginalPositionValidationData]
(
	[SecurityId]       INT				NOT NULL,
    [FundBookEntityId] INT				NOT NULL,
    [Date]             DATE				NOT NULL,
	[CloseExposureCheked] BIT NOT NULL DEFAULT 0, 
	[PositionPnLCheked] BIT NOT NULL DEFAULT 0, 
	[PositionFxPnLCheked] BIT NOT NULL DEFAULT 0, 
	[TradePnLCheked] BIT NOT NULL DEFAULT 0, 
	[TradeFxPnLCheked] BIT NOT NULL DEFAULT 0, 

    CONSTRAINT [FK_OriginalPositionValidationData_ToOriginalPosition] FOREIGN KEY ([FundBookEntityId], [SecurityId], [Date])
		 REFERENCES [TradingData].[OriginalPosition]([FundBookEntityId], [SecurityId], [Date])
)

GO

--Insert DefaultData

	INSERT into [TradingData].[OriginalPositionValidationData] ([SecurityId],[FundBookEntityId],[Date])
		select position.[SecurityId], position.[FundBookEntityId] ,position.[Date] from [TradingData].OriginalPosition as position 
		left join [TradingData].[OriginalPositionValidationData] as valid 
		on valid.[SecurityId] = position.[SecurityId] 
		and  valid.[FundBookEntityId] = position.[FundBookEntityId]
		and  valid.[Date] = position.[Date]
		where valid.[SecurityId] is null